var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var helperOpen = require("helper/open");
var qqHttp = require("helper/http");
var userUrl = require("url/user");
var addWithdrawUrl = userUrl.url.withdraw.add;

var bankUrl = userUrl.url.bank.hasUserBank;
var layoutBank = ui("layoutBank");
var do_Label_5 = ui("do_Label_5");
var do_Label_withdrawCash=ui("do_Label_withdrawCash");
var do_Button_to_withdraw=ui("do_Button_to_withdraw");
var do_TextField_winthdraw=ui("do_TextField_winthdraw");
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
var header = ui("header");
//andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
headerRight.visible = false;
headerLeft.on("touch", function() {
	do_App.closePage()
});
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "提现",
})
header.refreshData();
var bankVo;
do_Page.on("result", function(data) {
	if(data&&data.fromPage&&data.fromPage==="bank_select"){
		layoutBank.visible = true;
		do_Label_5.visible = false;
		bankVo=data.bankVo;
		ui("do_Label_6").text=bankVo.userReal;
		if(bankVo.type==1){
			ui("do_Label_7").text="支付宝";
			ui("do_Label_content").text=bankVo.accountNo;
		}else{
			ui("do_Label_7").text=bankVo.bankName+bankVo.subbranch;
			ui("do_Label_content").text=bankVo.accountNo;
			
		}
	}else if(data&&data.fromPage&&data.fromPage==="bank_add"){
		do_Label_5.visible = true;
		layoutBank.visible = false;
		loadPage();
	}
});
var withdrawCash;
// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	withdrawCash=do_Page.getData().withdrawCash;
	do_Label_withdrawCash.text=withdrawCash;
	loadPage();
});
var openPage="source://view/user/bank/add/main.ui";
function loadPage(){
	qqHttp.post(bankUrl, {}, function(data) {
		if(data.hasUserBank){
			do_Label_5.text="请选择银行卡";
			openPage="source://view/user/bank/select/selectRadio.ui";
		}else{
			do_Label_5.text="请添加银行卡";
		}
	})
}
ui("do_ALayout_14").on("touch",function(){
	helperOpen.open(openPage);
})


var type=1;
do_Button_to_withdraw.on("touch",function(data,e){
	do_Page.hideKeyboard();
	var to_winthdraw=do_TextField_winthdraw.text;
	if(null==to_winthdraw||""==to_winthdraw){
		nf.toast("请输入提现的金额。");
		return;
	}
	if(isNaN(to_winthdraw)){
		nf.toast("请输入正确的金额。");
		return;
	}
	var re = /^[1-9]+[0-9]*]*$/;  
    if (!re.test(to_winthdraw)){  
		nf.toast("请输入正整数。");  
        return;  
    }
    if(to_winthdraw<0){
    	nf.toast("请输入整数金额。");  
        return;
	}
    if(to_winthdraw % 100 != 0){
    	nf.toast("请输入100的整数倍金额。");  
        return;
	}
    if(null==bankVo){
    	nf.toast("请选择银行卡。");  
        return;
    }
    if(null==bankVo.userReal||""==bankVo.userReal.trim()){
    	nf.toast("提现账号真实姓名不能为空。");  
        return;
    }
    var retPhone = /^1\d{10}$/;
    var retEmall = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
    if(bankVo.type==1){
	    if(!retEmall.test(bankVo.accountNo)&&!retPhone.test(bankVo.accountNo)){
    		nf.toast("请选择正确支付宝账号，只能是邮箱或者手机号码。");  
	        return;
	    }
    }else{
    	if(isNaN(bankVo.accountNo)||bankVo.accountNo.length < 16||bankVo.accountNo.length > 20||bankVo.accountNo<0){
    		nf.toast("请选择正确银行卡账号，只能是16-20位的数字。");  
	        return;
	    }
    }
    var params = {
			"type" : bankVo.type,
			"cash" : to_winthdraw,
			"accountNo":bankVo.accountNo,
			"userReal":bankVo.userReal,
			"bankName":bankVo.bankName+bankVo.subbranch
		};
		qqHttp.post(addWithdrawUrl, params, function(data) {
			nf.toast("申请成功");
			do_App.closePage({withdrawCash:data.withdrawCash,fromPage:"withdrawCash"});
		})
})

